package com.controller;


import com.DTO.AddTwitterDTO;
import com.DTO.PageParam;
import com.DTO.UpdateTwitterDTO;
import com.Exception.NormalException.FileExceptions.FileFormatException;
import com.Result.TwitterWithURLs;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entity.Twitter;
import com.entity.User;
import com.service.FriendService;
import com.service.TwitterPictureService;
import com.service.TwitterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Queue;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XTC
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/twitter")
@CrossOrigin
@Api(tags = "朋友圈管理器")
public class TwitterController {
    @Autowired
    private TwitterService twitterService;

    @Autowired
    private TwitterPictureService twitterPictureService;

    @Autowired
    private FriendService friendService;

    @PostMapping("/add")
    @ApiOperation(value = "添加朋友圈")
    public boolean add (AddTwitterDTO addTwitterDTO, HttpServletResponse response) throws IOException {
        try {
            Twitter twitter = new Twitter();
            BeanUtils.copyProperties(addTwitterDTO, twitter);
            twitter.setPublishTime(new Date());
            this.twitterService.save(twitter);
            this.twitterPictureService.savePictures(twitter.getId(), addTwitterDTO.getUserId(), addTwitterDTO.getPictures());
        } catch (FileFormatException e) {
            e.setStatus(response);
            return false;
        }
        return true;
    }

    @GetMapping("/getByUserId")
    @ApiOperation(value = "获取某用户发表的朋友圈")
    public List<TwitterWithURLs> getByUserId (PageParam pageParam, Integer userId) {
        Page<Twitter> pages = this.twitterService.getByUserId(pageParam, userId);
        return this.twitterPictureService.setPictureURL(pages.getRecords());
    }

    @GetMapping("/getTwitterOfFriends")
    @ApiOperation(value = "获取朋友发表的朋友圈")
    public List<TwitterWithURLs> getTwitterOfFriends (PageParam pageParam, Integer userId) {
        List<Integer> friendIdList = this.friendService.getAllFriendIdOf(userId);
        Page<Twitter> pages = this.twitterService.getTwitterOfFriends(pageParam, friendIdList);
        return this.twitterPictureService.setPictureURL(pages.getRecords());
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有朋友圈")
    public List<Twitter> getAll () {
        return this.twitterService.list();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改朋友圈", notes = "通过id唯一确定朋友圈,不可修改发送人id和发送时间")
    @ApiResponses({
            @ApiResponse(code = FileFormatException.code, message = FileFormatException.message)
    })
    public boolean update(@RequestBody UpdateTwitterDTO updateTwitterDTO) throws FileFormatException, IOException {
        this.twitterService.update(updateTwitterDTO);
        this.twitterPictureService.savePictures(updateTwitterDTO.getId(), updateTwitterDTO.getUserId(), updateTwitterDTO.getPictures());
        return true;
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除朋友圈")
    public boolean delete (Integer twitterId) {
        return this.twitterService.delete(twitterId);
    }
}

